package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.financing.wap.common.Session;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.FaUserDTO;
import com.financing.wap.dto.FpShopSubjectDTO;
import com.financing.wap.dto.FpShopSubjectUserDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpShopSubjectService;
import com.financing.wap.service.IFpShopSubjectUserService;
import com.financing.wap.service.IUserService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller("fpShopSubjectUserController")
@RequestMapping("/api/fpShopSubjectUser")
public class FpShopSubjectUserController extends BaseController {

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IFpShopSubjectUserService fpShopSubjectUserService;
	@Autowired
	private IFpShopSubjectService fpShopSubjectService;
	
	@Autowired
	private IUserService fpFaUserService;
	
	@RequestMapping("/listFpShopSubjectUser")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser, Pager pager) {
		ResultMap result = new ResultMap();

		try {
			logger.info("query fpShopSubjectUser list begin .......");

			// fpShopSubjectUser.setBegin(pager.getStartIndex());
			// fpShopSubjectUser.setPageSize(pager.getPageSize());

			int count = fpShopSubjectUserService.queryListCount(fpShopSubjectUser);

			pager.setTotalCount(count);

			List<FpShopSubjectUserDTO> fpShopSubjectUserList = null;

			if (count > 0) {
				fpShopSubjectUserList = fpShopSubjectUserService.queryList(fpShopSubjectUser);
			} else {
				fpShopSubjectUserList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopSubjectUserList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query fpShopSubjectUser list end .......");
		} catch (Exception e) {
			log.error("query fpShopSubjectUser error!", e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/addFpShopSubjectUser")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser) {
		ResultMap result = new ResultMap();

		try {
			logger.info("add fpShopSubjectUser  begin .......");

			DTOdateSetUtil.setCreateInfo(fpShopSubjectUser);
			DTOdateSetUtil.setUpdateInfo(fpShopSubjectUser);

			String js=request.getParameter("qajson");
			if (StringUtils.isBlank(js)) {
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			//"[{'tid':386,'sid':'A'},{'tid':388,'sid':'B'},{'tid':1,'sid':'C'},{'tid':3,'sid':'B'}]"
			JSONArray jas = JSONArray.fromObject(js);
			
			//tid 题目  sid 答案
			String anwers = "";
			String dana="";
			for (int i = 0; i < jas.size(); i++) {
				anwers+=","+JSONObject.fromObject(jas.get(i)).get("tid").toString();
				dana+=","+JSONObject.fromObject(jas.get(i)).get("sid").toString();
			}
			if(anwers.length()>0){
				anwers=anwers.substring(1,anwers.length());
			}
			if(dana.length()>0){
				dana=dana.substring(1,dana.length());
			}
			fpShopSubjectUser.setAnswerList(dana);
			fpShopSubjectUser.setSubjectList(anwers);
			if (fpShopSubjectUser.getAnswerList().split(",").length != fpShopSubjectUser.getSubjectList()
					.split(",").length) {
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_TYPE_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			// 查询题目正确性
			FpShopSubjectDTO subDto = new FpShopSubjectDTO();
			subDto.setShopSubjectIds(fpShopSubjectUser.getSubjectList());
			List<FpShopSubjectDTO> sublist = fpShopSubjectService.queryList(subDto);
			int a = 0;

			String tm[] = fpShopSubjectUser.getSubjectList().split(",");
			String da[] = fpShopSubjectUser.getAnswerList().split(",");
			for (int j = 0; j < da.length; j++) {
				for (int i = 0; i < sublist.size(); i++) {
					if (Integer.parseInt(tm[j])==sublist.get(i).getShopSubjectId()) {
						// 判断答案
						
						if (da[j].equals(sublist.get(i).getTrueAnswer())) {
							a++;
						}
					}
				}
			}
			fpShopSubjectUser.setAnswerFration(a + ""); // 设置分数 4 分为满分
			
			Session session = getSession(true);
			UserDTO loginUser = session.getUser();
			fpShopSubjectUser.setULoginName(loginUser.getLoginName()); // 获取用户电话
			
			
			//满分直接通过认证
			if(a==4 && (StringUtils.isBlank(loginUser.getRole()) ||
					!loginUser.getRole().equals("2"))){
				FaUserDTO use = new FaUserDTO();
				use.setFpTempRole("2"); // 答题标识
				use.setLoginNum(loginUser.getLoginNum());
				use.setuRole("2");  // 
				DTOdateSetUtil.setUpdateInfo(use);
				use.setId(loginUser.getId());
				fpFaUserService.updateFaUserInfo(use);
				
				//修改自动通过
				UserDTO userDto=new UserDTO();
				userDto.setId(loginUser.getId());
				fpFaUserService.updatefaUserInfoAutoPass(userDto);
			}
		

			fpShopSubjectUserService.insert(fpShopSubjectUser);
			result.setAttribute("trueNum", a);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("add fpShopSubjectUser list end .......");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("add fpShopSubjectUser error!", e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/detailFpShopSubjectUser")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser) {
		ResultMap result = new ResultMap();

		try {
			logger.info("query detail fpShopSubjectUser  begin .......");

			Integer id = fpShopSubjectUser.getId();

			if (id == null) {
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			FpShopSubjectUserDTO dto = fpShopSubjectUserService.loadById(id);

			result.setAttribute("dto", dto);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query detail fpShopSubjectUser  end .......");
		} catch (Exception e) {
			log.error("query detail fpShopSubjectUser error!", e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/deleteFpShopSubjectUser")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser) {
		ResultMap result = new ResultMap();

		try {
			logger.info("delete fpShopSubjectUser  begin .......");

			Integer id = fpShopSubjectUser.getId();

			if (id == null) {
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			fpShopSubjectUserService.delete(id);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("delete fpShopSubjectUser  end .......");
		} catch (Exception e) {
			log.error("delete fpShopSubjectUser error!", e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/updateFpShopSubjectUser")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser) {
		ResultMap result = new ResultMap();

		try {
			logger.info("update fpShopSubjectUser  begin .......");

			Integer id = fpShopSubjectUser.getId();

			if (id == null) {
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSetUtil.setUpdateInfo(fpShopSubjectUser);

			fpShopSubjectUserService.update(fpShopSubjectUser);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("update fpShopSubjectUser list end .......");
		} catch (Exception e) {
			log.error("update fpShopSubjectUser error!", e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/updateForceFpShopSubjectUser")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser) {
		ResultMap result = new ResultMap();

		try {
			logger.info("updateForce fpShopSubjectUser  begin .......");

			Integer id = fpShopSubjectUser.getId();

			if (id == null) {
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSetUtil.setUpdateInfo(fpShopSubjectUser);

			fpShopSubjectUserService.updateForce(fpShopSubjectUser);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("updateForce fpShopSubjectUser list end .......");
		} catch (Exception e) {
			log.error("updateForce fpShopSubjectUser error!", e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

}
